package com.keeping.aiimagery.Config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.keeping.aiimagery.entity.User;
import org.apache.catalina.security.SecurityUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * Created by Ranger on 2023/4/2.
 */
@Component
public class ChatMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        User adminAuthUser = new User();
        adminAuthUser.setId(1);
        //TODO 获取当前用户信息
        if(ObjectUtils.isNotEmpty(adminAuthUser)){
            this.setFieldValByName("createdBy", adminAuthUser.getId(), metaObject);
            this.setFieldValByName("modifyBy", adminAuthUser.getId(), metaObject);
        }else{
            Object createdBy = this.getFieldValByName("createdBy", metaObject);
            if(createdBy == null){
                this.setFieldValByName("createdBy", 0L, metaObject);
            }
            Object modifyBy = this.getFieldValByName("modifyBy", metaObject);
            if(modifyBy == null){
                this.setFieldValByName("modifyBy", 0L, metaObject);
            }
        }
        this.setFieldValByName("version", 0L, metaObject);
        this.setFieldValByName("deleted", 0, metaObject);
        this.setFieldValByName("createdTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("modifyTime", LocalDateTime.now(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        User adminAuthUser = new User();
        //TODO 获取当前用户信息
        if(ObjectUtils.isNotEmpty(adminAuthUser)){
            this.setFieldValByName("createdBy", adminAuthUser.getId(), metaObject);
            this.setFieldValByName("modifyBy", adminAuthUser.getId(), metaObject);
        }else{
            this.setFieldValByName("createdBy", 0L, metaObject);
            this.setFieldValByName("modifyBy", 0L, metaObject);
        }
    }
}
